Package com.apps.utils

Source Code of com.apps.utils.SMSUtils

package com.apps.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.http.client.ClientProtocolException;
import org.apache.tools.ant.util.Base64Converter;

public class SMSUtils {
 
  public static void sendSMS(String number, String msg){
        try {
          URL url = new URL ("https://api.twilio.com/2010-04-01/Accounts/ACb80245f1512e4f2c83a550a469b40b0a/SMS/Messages.Xml");
          Base64Converter converter = new Base64Converter();
          String encoding = converter.encode("ACb80245f1512e4f2c83a550a469b40b0a:a852b10fc203462dd38367464ead42b1");
         

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setInstanceFollowRedirects(true);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
            connection.setRequestProperty("Accept", "*/*");
            connection.setDefaultUseCaches (false);
            connection.setRequestProperty("Authorization", "Basic " + encoding);
            String encodedMsg = URLEncoder.encode(msg,"UTF-8");
            byte[] outputBytes = ("From=%2B12064625899&To=%2B1"+number+"&Body="+encodedMsg).getBytes("UTF-8");
            OutputStream os = connection.getOutputStream();
            os.write(outputBytes);
            os.flush();
            os.close();

            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = br.readLine();
            while(line !=null){System.out.println(line); line = br.readLine();}

           
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      } catch (ClientProtocolException e) {
        e.printStackTrace();
      } catch (ProtocolException e) {
        e.printStackTrace();
      } catch (IOException e) {
       
        e.printStackTrace();
      }
        
  }

}
TOP

Related Classes of com.apps.utils.SMSUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.